import swaggerJSDoc from 'swagger-jsdoc';
import { config } from './env.js';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rootDir = path.join(__dirname, '..', '..');

/**
 * Swagger配置选项
 */
const swaggerOptions: swaggerJSDoc.Options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: config.swagger.title,
      version: config.swagger.version,
      description: config.swagger.description,
      contact: {
        name: 'API Support',
        email: 'support@example.com',
      },
      license: {
        name: 'MIT',
        url: 'https://opensource.org/licenses/MIT',
      },
    },
    servers: [
      {
        url: config.swagger.host || `http://localhost:${config.app.port}`,
        description: config.app.isProd ? 'Production server' : 'Development server',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        },
        sessionAuth: {
          type: 'apiKey',
          in: 'cookie',
          name: config.auth.session.key,
        },
      },
      schemas: {
        Error: {
          type: 'object',
          properties: {
            code: {
              type: 'integer',
              description: '错误代码',
            },
            message: {
              type: 'string',
              description: '错误信息',
            },
            timestamp: {
              type: 'integer',
              description: '时间戳',
            },
          },
        },
        Success: {
          type: 'object',
          properties: {
            code: {
              type: 'integer',
              example: 200,
              description: '成功代码',
            },
            message: {
              type: 'string',
              example: 'success',
              description: '成功信息',
            },
            timestamp: {
              type: 'integer',
              description: '时间戳',
            },
            data: {
              description: '响应数据',
            },
          },
        },
        Pagination: {
          type: 'object',
          properties: {
            code: {
              type: 'integer',
              example: 200,
            },
            message: {
              type: 'string',
              example: 'success',
            },
            timestamp: {
              type: 'integer',
            },
            data: {
              type: 'object',
              properties: {
                total: {
                  type: 'integer',
                  description: '总数量',
                },
                page: {
                  type: 'integer',
                  description: '当前页码',
                },
                pageSize: {
                  type: 'integer',
                  description: '每页数量',
                },
                list: {
                  type: 'array',
                  description: '数据列表',
                  items: {},
                },
              },
            },
          },
        },
        // 注意：具体的schema定义已移至 docs/components.yaml 文件中
        // 这里保留基础的响应结构定义，具体的业务模型在yaml文件中定义
      },
    },
  },
  apis: [
    // 扫描独立的swagger文档文件
    path.join(rootDir, 'src/docs/**/*.yaml'),
    path.join(rootDir, 'src/docs/**/*.yml'),
  ],
};

/**
 * 生成Swagger文档
 */
export const swaggerSpec = swaggerJSDoc(swaggerOptions);

/**
 * 获取Swagger配置
 */
export function getSwaggerConfig() {
  return {
    enabled: config.swagger.enabled,
    path: config.swagger.path,
    spec: swaggerSpec,
  };
}
